﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string CRM_CKind_Add(CRM_CKind model ,int ACID);


[OperationContract]
string CRM_CKind_Update(CRM_CKind model ,int ACID);


[OperationContract]
string CRM_CKindByKindCode_Delete(string KindCode,int ACID);


[OperationContract]
CRM_CKind CRM_CKindByKindCode_Get(string KindCode,int ACID );


[OperationContract]
List<CRM_CKind> CRM_CKind_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加CRM_CKind表数据
/// <summary>
/// 添加CRM_CKind
/// </summary>
/// <param name="model">CRM_CKind实体</param>
/// <returns>空或错误</returns>
public string CRM_CKind_Add(CRM_CKind model,int ACID)
{
if(string.IsNullOrEmpty(model.KindCode))
{
return "『客户类型ID』不能为空！";
}
if(string.IsNullOrEmpty(model.KindName))
{
return "『客户类型名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_CKind.Count(p=>p.KindCode==model.KindCode)>0)
{
return "主键已存在！";
}
if(ctx.CRM_CKind.Count(p=>p.KindName==model.KindName)>0)
{
return "客户类型名称已存在！";
}
ctx.CRM_CKind.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加CRM_CKind表数据事物用重构
/// <summary>
/// 添加CRM_CKind事物用重构
/// </summary>
/// <param name="model">CRM_CKind实体</param>
/// <returns>空或错误</returns>
private string CRM_CKind_Add(CRM_CKind model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.KindCode))
{
return "『客户类型ID』不能为空！";
}
if(string.IsNullOrEmpty(model.KindName))
{
return "『客户类型名称』不能为空！";
}
if(ctx.CRM_CKind.Count(p=>p.KindCode==model.KindCode)>0)
{
return "主键已存在！";
}
if(ctx.CRM_CKind.Count(p=>p.KindName==model.KindName)>0)
{
return "客户类型名称已存在！";
}
ctx.CRM_CKind.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改CRM_CKind表数据
/// <summary>
/// 修改CRM_CKind
/// </summary>
/// <param name="model">CRM_CKind实体</param>
/// <returns>空或错误</returns>
public string CRM_CKind_Update(CRM_CKind model,int ACID)
{
if(string.IsNullOrEmpty(model.KindCode))
{
return "『客户类型ID』不能为空！";
}
if(string.IsNullOrEmpty(model.KindName))
{
return "『客户类型名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_CKind.Count(p=>p.KindCode==model.KindCode)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
CRM_CKind CRM_CKindmodel=ctx.CRM_CKind.Single(p => p.KindCode==model.KindCode);
if(!string.IsNullOrEmpty(model.KindCode))
{
CRM_CKindmodel.KindCode=model.KindCode;
}
if(!string.IsNullOrEmpty(model.KindName))
{
CRM_CKindmodel.KindName=model.KindName;
}
if(!string.IsNullOrEmpty(model.ParentCode))
{
CRM_CKindmodel.ParentCode=model.ParentCode;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除CRM_CKind表ByKindCode数据
/// <summary>
/// 删除CRM_CKindByKindCode
/// </summary>
/// <param name="KindCode">客户类型ID</param>
/// <returns>空或错误</returns>
public string CRM_CKindByKindCode_Delete(string KindCode,int ACID)
{
if(string.IsNullOrEmpty(KindCode))
{
return "『客户类型ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_CKind.Count(p=>p.KindCode==KindCode)!=1)
{
return null;
}
CRM_CKind model=ctx.CRM_CKind.Single(p => p.KindCode== KindCode);
ctx.CRM_CKind.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除CRM_CKind表ByKindCode数据[事物用重构]
/// <summary>
/// 删除CRM_CKindByKindCode[事物用重构]
/// </summary>
/// <param name="KindCode">客户类型ID</param>
/// <returns>空或错误</returns>
private string CRM_CKindByKindCode_Delete(string KindCode,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(KindCode))
{
return "『客户类型ID』不能为空！";
}
if(ctx.CRM_CKind.Count(p=>p.KindCode==KindCode)!=1)
{
return null;
}
CRM_CKind model=ctx.CRM_CKind.Single(p => p.KindCode== KindCode);
ctx.CRM_CKind.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取CRM_CKind表ByKindCode数据
/// <summary>
/// 获取CRM_CKindByKindCode
/// </summary>
/// <param name="KindCode">客户类型ID</param>
/// <returns>当前表</returns>
public CRM_CKind CRM_CKindByKindCode_Get(string KindCode,int ACID)
{
if(string.IsNullOrEmpty(KindCode))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_CKind.Count(p=>p.KindCode==KindCode)!=1)
{
return null;
}
return ctx.CRM_CKind.SingleOrDefault(p=>p.KindCode==KindCode);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取CRM_CKind表数据
/// <summary>
/// 获取CRM_CKind
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_CKind></returns>
public List<CRM_CKind> CRM_CKind_Get(Expression<Func<CRM_CKind, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_CKind.ToList();
}
 else 
{
return ctx.CRM_CKind.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_CKind.Where(predicate).ToList();
}
 else 
{
return ctx.CRM_CKind.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取CRM_CKind表数据
/// <summary>
/// 获取CRM_CKind
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_CKind></returns>
public List<CRM_CKind> CRM_CKind_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return CRM_CKind_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取CRM_CKind表数据计数
/// <summary>
/// 获取CRM_CKind表数据计数
/// </summary>
/// <returns>List<CRM_CKind></returns>
public int CRM_CKind_GetCount(Expression<Func<CRM_CKind, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.CRM_CKind.Count();

}
 else
{
return ctx.CRM_CKind.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取CRM_CKind分页表数据
/// <summary>
/// 获取CRM_CKind分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_CKind></returns>
public List<CRM_CKind> CRM_CKind_Getpage(int pageIndex, int pageSize,Expression<Func<CRM_CKind, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_CKind.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CRM_CKind.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_CKind.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CRM_CKind.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改CRM_CKind表数据
/// <summary>
/// 批量修改CRM_CKind
/// </summary>
/// <param name="predicate">Expression<Func<CRM_CKind, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CRM_CKind,CRM_CKind>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int CRM_CKind_batchUpdate(Expression<Func<CRM_CKind, bool>> predicate,Expression<Func<CRM_CKind, CRM_CKind>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CRM_CKind.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改CRM_CKind表数据事物用重构
/// <summary>
/// 批量修改CRM_CKind事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CRM_CKind, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CRM_CKind,CRM_CKind>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CRM_CKind_batchUpdate(Expression<Func<CRM_CKind, bool>> predicate,Expression<Func<CRM_CKind, CRM_CKind>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CRM_CKind.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除CRM_CKind表数据
/// <summary>
/// 批量删除CRM_CKind
/// </summary>
/// <param name="predicate">Expression<Func<CRM_CKind, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int CRM_CKind_batchDel(Expression<Func<CRM_CKind, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CRM_CKind.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除CRM_CKind表数据事物用重构
/// <summary>
/// 批量删除CRM_CKind事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CRM_CKind, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CRM_CKind_batchDel(Expression<Func<CRM_CKind, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CRM_CKind.Delete(predicate);
return ok;
}
#endregion


}
}

